package com.srit.map.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


public class HttpClient {
	public static HttpResponse response;
	public static DefaultHttpClient httpClient;

	public HttpClient(){
		httpClient = new DefaultHttpClient();
		HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
	}
	
	public HttpClient(String userName, String password) throws Exception {
		httpClient = new DefaultHttpClient();
		HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
		this.Login2(userName, password);
	}
	
	// ��ָ̽��ҳ��Ĵ���
	public String notify(String url) {
		HttpGet get = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String txt = null;
		try {
			txt = httpClient.execute(get, responseHandler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			get.abort();
		}
		return txt;
	}

	// ��post��������������� �������Ӧ����Ϊpost����Ҫ��װ��������ں����ⲿ��װ�ô���
	public HttpResponse postMethod(HttpPost post) {
		HttpResponse resp = null;

		try {
			resp = httpClient.execute(post);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			post.abort();
		}
		return resp;
	}

	public String getCookie() {
		String cookie = "";
		List<Cookie> cl = httpClient.getCookieStore().getCookies();
		for (int i = 0; i < cl.size(); i++) {
			cookie += cl.get(i).getName() + "=" + cl.get(i).getValue() + "D-E-M-O-N";
		}
		return cookie;
	}

	public void setCookie(String cx) {
		
		String[] cook = cx.split("D-E-M-O-N");
		httpClient.getCookieStore().clear();
		String t = "";
		boolean ist = false;
		CookieStore cookies = httpClient.getCookieStore();
		for (int i = 0; i < cook.length - 1; i++) {
			String[] tmp = cook[i].split("=");
			
			if(tmp[0].contains("societyguester")) t = tmp[1];
			if(tmp[0].equals("t")) ist = true;
			BasicClientCookie cookie = new BasicClientCookie(tmp[0], tmp[1]);
			cookie.setDomain(".renren.com");
			cookie.setPath("/");
			cookies.addCookie(cookie);
		}
		if(!ist){
			
			BasicClientCookie cookie = new BasicClientCookie("t",t);
			cookie.setDomain(".renren.com");
			cookie.setPath("/");
			cookies.addCookie(cookie);
			
		}
		
		httpClient.setCookieStore(cookies);
	}

	public String postMethod2(HttpPost post) {
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String txt = null;
		try {
			txt = httpClient.execute(post, responseHandler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			post.abort();
		}
		return txt;
	}

	// ��get��������������� �������Ӧ
	public HttpResponse getMethod(String url) {
		HttpGet get = new HttpGet(url);
		HttpResponse resp = null;
		try {
			resp = httpClient.execute(get);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			get.abort();
		}
		return resp;
	}


	public void Login2(String userName, String password) {
		String redirectURL = "http://www.renren.com/home";

		// Don't change the following URL
		String renRenLoginURL = "http://www.renren.com/ajaxLogin/login";

		httpClient = new DefaultHttpClient();
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		HttpPost httpost = new HttpPost(renRenLoginURL);
		// All the parameters post to the web site
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("origURL", redirectURL));
		nvps.add(new BasicNameValuePair("domain", "renren.com"));
		nvps.add(new BasicNameValuePair("key_id", "1"));
		nvps.add(new BasicNameValuePair("_rtk", "eaf03ff3"));
		nvps.add(new BasicNameValuePair("icode", ""));
		nvps.add(new BasicNameValuePair("submit", "��¼"));
		nvps.add(new BasicNameValuePair("email", userName));
		nvps.add(new BasicNameValuePair("password", password));
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			String txt = httpClient.execute(httpost, responseHandler);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpost.abort();
		}
		String redirectLocation = getRedirectLocation();
		if (redirectLocation != null) {
			// System.out.println(getText(redirectLocation));
			// ����ҳ�����ڵ�¼���
			getText(redirectLocation);
		}
		
	}
	
	
	public void Login(String userName, String password) {

		String redirectURL = "http%3A%2F%2Fapps.renren.com%2Fpetparty%2F%3Forigin%3D103%26guide";
		// Don't change the following URL
		String renRenLoginURL = "http://www.renren.com/PLogin.do";

		httpClient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(renRenLoginURL);
		// All the parameters post to the web site
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("origURL", redirectURL));
		nvps.add(new BasicNameValuePair("email", userName));
		nvps.add(new BasicNameValuePair("password", password));
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			response = httpClient.execute(httpost);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpost.abort();
		}
		String redirectLocation = getRedirectLocation();
		if (redirectLocation != null) {
			// System.out.println(getText(redirectLocation));
			// ����ҳ�����ڵ�¼���
			getText(redirectLocation);
		}
	}
	private String getRedirectLocation() {
		Header locationHeader = response.getFirstHeader("Location");
		if (locationHeader == null) {
			return null;
		}
		return locationHeader.getValue();
	}

	private String getText(String redirectLocation) {
		HttpGet httpget = new HttpGet(redirectLocation);
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		try {
			responseBody = httpClient.execute(httpget, responseHandler);
		} catch (Exception e) {
			e.printStackTrace();
			responseBody = null;
		} finally {
			httpget.abort();
			// httpclient.getConnectionManager().shutdown();
		}
		return responseBody;
	}
	
	
}
